﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NCLCustomerManagement.DataTransferObjects.User;
using My.DataAccess.Core;
using My.Common.Helper.Utils;

namespace NCLCustomerManagement.DataAccess.User
{
    public class UserRepository : BaseRepository<UserProfileDTO, UserProfile>, IUserRepository
    {
        public bool Authenticate(string userName, string password)
        {
            var auth = UnitOfWork.Table<UserProfile>().Any(e => e.UserName == userName
                && e.Password == password && e.Active.Value);
            return auth;
        }


        public bool IsAdmin(string username)
        {
            var user = UnitOfWork.Table<UserProfile>().SingleOrDefault(e => e.UserName == username);
            if (user == null)
                return false;
            return user.IsAdmin;
        }




        public bool Authenticate(string userName, string password, out UserProfile userProfile)
        {
            bool auth = Authenticate(userName, password);
            userProfile = UnitOfWork.Table<UserProfile>().SingleOrDefault(e => e.UserName == userName && e.Password == password);
            return auth;
        }


        public IEnumerable<UserProfileDTO> GetUserListOf(string manager)
        {
            var userList = UnitOfWork.Table<UserProfile>()
                .Where(e => e.ReportTo == manager)
                .AsEnumerable()
                .Select(e => ConvertEntityToDTO(e));
            return userList;

        }

        public UserProfileDTO GetUserByUsername(string username)
        {
            var user = GetUser(username);
            return ConvertEntityToDTO(user);
        }

        public void AddNewUser(UserProfileDTO userDto)
        {
            UserProfile user = ConvertDTOToEntity(userDto);
            UnitOfWork.InsertOnSubmit<UserProfile>(user);
            UnitOfWork.SubmitChanges();
        }

        public void DeleteUser(string username)
        {
            UserProfile user = GetUser(username);
            foreach (var item in user.UserRequests)
            {
                UnitOfWork.DeleteOnSubmit<UserRequest>(item);
            }
            UnitOfWork.DeleteOnSubmit<UserProfile>(user);
            UnitOfWork.SubmitChanges();
        }


        //public void UpdateUser(UserProfileDTO userDto)
        //{
        //    var user = GetUser(userDto.Username);
        //    user.Active = userDto.Active;
        //    user.Email = userDto.Email;
        //    user.IsAdmin = userDto.IsAdmin;
        //    user.Password = userDto.Password;
        //    user.ReportTo = userDto.ReportTo;

        //    UnitOfWork.SubmitChanges();
        //}

        private UserProfile GetUser(string username)
        {
            return UnitOfWork.Table<UserProfile>().SingleOrDefault(e => e.UserName == username);
        }


        public void ChangePassword(string username, string newPassword)
        {
            var user = GetUser(username);
            user.Password = newPassword;
            UnitOfWork.SubmitChanges();
        }


        public void ActivateDeactivateUser(string username, bool activate)
        {
            var user = GetUser(username);
            user.Active = activate;
            UnitOfWork.SubmitChanges();
        }
    }
}
